<?php
/**
 * Created by PhpStorm.
 * User: 张福来
 * Date: 2018/10/29
 * Time: 14:51
 */
namespace app\api\controller;
use app\api\model\Message;
use app\api\model\User as UserModel;
use app\api\validate\Apivalidate;
use app\api\controller\Map;
use think\Request;
use app\reserve\model\Order;

class User extends Base{
    /*
    * Notes: 用户查询
    * Create Date: 2018/10/29
    * Create Time: 15:27
    * Create by :张福来
    * $pageNum : 页码
    * $listNum : 查询条数 默认10条
    */
    public function selectUser(Request $request){
        $postData = $request->post();
        $check = $this->validate($postData,"UserValidate.selectUser");
        if($check !== true){
            return apiResponse(200,$check,"");
        }
        $user = new UserModel;
        if(empty($postData["pagenum"])){
            $pageNum = 1;
        }else{
            $pageNum = $postData["pagenum"];
        }
        if(empty($postData["listNum"])){
            $listNum = 10;
        }else{
            $listNum = $postData["listNum"];
        }
        if(!empty($postData["u_id"])){
            $w["u_id"] = $postData["u_id"];
        }else{
            $w["u_id"] = array('NEQ',0);
        }
        if(!empty($postData["p_id"])){
            $w["p_id"] = $postData["p_id"];
        }
        $w["status"] = array('NEQ',9);
        if(!empty($postData["nickName"])){
            $w["nickName"] = array('LIKE','%'.trim($postData["nickName"]).'%');
        }
        $result = $user->selecUser($pageNum,$listNum,$w);
//        $count = count($result["list"]);
//        for($i = 0; $i<$count;$i++){
//            $result["list"][$i]["ctime"] = date("Y-m-d H:i:s", $result["list"][$i]["ctime"]);
//        }
        if (!empty($result['list'])){
            return apiResponse(200,"查询成功",$result);
        }else{
            return apiResponse(200,"数据为空",$result);
        }
    }
    /*
    * Notes: 编辑用户信息
    * Create Date: 2018/10/5
    * Create Time: 下午5:20
    * Create by :张福来
    * $addarray :  要添加的数据
    */
    public function eidtUser(Request $request){
        $user = new UserModel;
        $addarray = $request->post();
        $check = $this->validate($addarray,"UserValidate.eidtUser");
        if($check !== true){
            return apiResponse(201,$check,"");
        }
        $addarray["utime"] = time();
        $result = $user->eidtUser($addarray);
        if($result){
            return apiResponse(200,"修改成功",$result);
        }else{
            return apiResponse(201,"修改失败",$result);
        }
    }


    /*
    * Notes: 编辑用户信息
    * Create Date: 2018/10/5
    * Create Time: 下午5:20
    * Create by :张福来
    * $addarray :  要添加的数据
    */
    public function userInfo(Request $request){
        $user = new UserModel;
        $addarray = $request->post();
        $check = $this->validate($addarray,"UserValidate.userInfo");
        if($check !== true){
            return apiResponse(201,$check,"");
        }
        $result = $user->where($addarray)->find();
        if($result){
            return apiResponse(200,"修改成功",$result);
        }else{
            return apiResponse(201,"修改失败",$result);
        }
    }

    /*
    * Notes: 用户签到
    * Create Date: 2018/10/5
    * Create Time: 下午6:53
    * Create by :张福来
    * $addarray["u_id"] :  用户id
    * $addarray["lon"] :  经度
    * $addarray["lat"] :  纬度
    * $addarray["ctime"] :  签到时间
    */
    public function userSign(){
        $user = new UserModel;
        $addarray["u_id"] = input("post.u_id");
        $addarray["lon"] = input("post.lon");
        $addarray["lat"] = input("post.lat");
        $addarray["ctime"] = time();
        $addarray["status"] = 1;
        if(empty($u_id)) apiResponse(201,"用户id不能为空","");
        if(empty($lon)) apiResponse(201,"经度不能为空","");
        if(empty($lat)) apiResponse(201,"纬度不能为空","");
        $w["u_id"] = $addarray["u_id"];
        $checkDayStr = date('Y-m-d ',time());
        $timeBegin = strtotime($checkDayStr." 00:00:00");
        $timeEnd = strtotime($checkDayStr." 23:59:59");
        $w['ctime'] = array(array('EGT',$timeBegin),array('ELT',$timeEnd),'and');
        $judgeSign = $user->judgeSign($w);
        if(!$judgeSign){
            $result = $user->userSign($addarray);
        }else{
            return apiResponse(201,"用户已经签到","");
        }
        if($result){
            $about = new About();
            $aboutarray = $about->index();
            $wallet = $aboutarray["data"][0]["sign_wallet"];
            $result2 = $this->walletoperate($addarray["u_id"],1,$wallet,"签到奖励");
            if($result2){
                $this->message(1,$addarray["u_id"],"签到","签到成功");
                return apiResponse(200,"签到成功",$result2);
            }else{
                $this->message(1,$addarray["u_id"],"签到","签到失败");
                return apiResponse(201,"签到失败",$result2);
            }
        }else{
            $this->message(1,$addarray["u_id"],"签到","签到失败");
            return apiResponse(201,"签到失败",$result);
        }

    }
    /*
    * Notes: 签到列表
    * Create Date: 2018/10/10
    * Create Time: 上午9:02
    * Create by :张福来
    * u_id : 用户id
    * pagenum : 页数
    * listNum : 条数
    */
    public function signList(){
        $user = new UserModel;
        $map = new Map();
        $w["u_id"] = input("post.u_id");
        $pageNum = input("post.pagenum");
        $listNum = input("post.listNum");
        if(empty($w["u_id"])) return apiResponse(200,"用户id 不能为空","");
        $result = $user -> signList($pageNum,$listNum,$w);
        $count = count($result["list"]);
        for($i = 0; $i<$count;$i++){
            if(!empty($result["list"][$i]["lat"]) && !empty($result["list"][$i]["lon"])){
                $result["list"][$i]["address"] = $map->address( $result["list"][$i]["lat"],$result["list"][$i]["lon"]);
            }else{
                $result["list"][$i]["address"] = "未知区域";
            }
            $result["list"][$i]["ctime"] = date("Y-m-d H:i:s",$result["list"][$i]["ctime"]);
        }
        if(empty($result["list"])){
            return apiResponse(200,"当前无数据","");
        }else{
            return apiResponse(200,"查询成功",$result);
        }
    }
    /*
    * Notes: 判断用户是否已经签到
    * Create Date: 2018/10/10
    * Create Time: 上午9:09
    * Create by :张福来
    * $curr_time : 判断时间 需要 时间戳的形式
    * $timeBegin : 当天的 0点
    * $timeEnd : 当天的 23点59秒59分
    */
    public function judgeSign(){
        $user = new UserModel;
        $curr_time = input("post.nowtime"); //传入时间
        $w["u_id"] = input("post.u_id");
        if(empty($w["u_id"])) return apiResponse(201,"用户id 不能为空","");
        if(empty($curr_time)) return apiResponse(201,"时间 不能为空","");
        $checkDayStr = date('Y-m-d ',$curr_time);
        $timeBegin = strtotime($checkDayStr." 00:00:00");
        $timeEnd = strtotime($checkDayStr." 23:59:59");
        $w['ctime'] = array(array('EGT',$timeBegin),array('ELT',$timeEnd),'and');
        $result = $user-> judgeSign($w);
        if($result){
            return apiResponse(201,"用户已经签到，不可以签到",$result);
        }else{
            return apiResponse(200,"用户未签到，可以签到",$result);
        }
    }
    /*
    * Notes: 用户积分明细账单列表查询
    * Create Date: 2018/10/5
    * Create Time: 下午8:15
    * Create by :张福来
    * $pageNum : 页数
    * $listNum : 条数
    * status : 状态
    */
    public function walletlist(){
        $pageNum = input("post.pagenum");
        $listNum = input("post.listNum");
        $w["status"] = array('NEQ',9);
        if(!empty(input("post.u_id"))) $w["u_id"] = input("post.u_id");
        if(empty($listNum)) $listNum = 10;
        if(!empty(input("post.name"))){
            $w['name'] = array('LIKE','%'.trim(input("post.name")).'%');
        }
        if(!empty(input("post.status"))){
            $w["status"] = input("post.status");
        }
        if(!empty(input('post.start_time')) && !empty(input('post.end_time'))){
            $start_time = input('post.start_time');
            $end_time = input('post.end_time');
            $w['ctime'] = array(array('EGT',strtotime($start_time)),array('ELT',strtotime($end_time)),'and');
        }
        if(!empty(input("type"))) $w["type"] = input("post.type");
        $user = new UserModel;
        $result = $user -> walletlist($pageNum,$listNum,$w);
        if(empty($result["list"])){
            return apiResponse(200,"查询成功",$result);
        }else{
            return apiResponse(200,"数据为空",$result);
        }
    }
    /*
    * Notes: 消息列表查询
    * Create Date: 2018/10/8
    * Create Time: 下午2:17
    * Create by :张福来
    * $pageNum : 页数
    * $listNum : 条数 默认10条
    */
    public function messageList(){
        $message = new Message();
        $u_id = input("post.u_id");
        if(empty($u_id)) return apiResponse(201,"用户id 不能为空","");
        $pageNum = input("post.pagenum");
        $listNum = input("post.listNum");
        if(!empty(input("post.status"))){
            $w["status"] =  input("post.status");
        }else{
            $w["status"] = array('NEQ',9);
        }
        $w["u_id"] = $u_id;
        $result = $message -> messageList($pageNum,$listNum,$w);
        if(!empty($result["list"])){
            return apiResponse(200,"查询成功",$result);
        }else{
            return apiResponse(200,"数据为空",$result);
        }
    }
    /*
    * Notes:  改变提醒信息
    * Create Date: 2018/10/8
    * Create Time: 下午2:32
    * Create by :张福来
    * id : 提醒信息的id
    * status : 状态 1已读 9 删除
    */
    public function changeMessage(){
        $message = new Message();
        $w["id"] = input("post.id");
        $c["status"] = input("post.status");
        $c["utime"] = time();
        if(empty($w["id"])) return apiResponse(201,"id 不能为空","");
        $result = $message->changeMessage($w,$c);
        if($result){
            return apiResponse(200,"成功",$result);
        }else{
            return apiResponse(201,"失败",$result);
        }
    }
    /*
    * Notes: 更绑手机号
    * Create Date: 2018/10/8
    * Create Time: 下午3:07
    * Create by :张福来
    * $u_id : 用户id
    * $phone : 要绑定的手机号码
    * $code : 验证码
    * changephone : 缓存cache键值
    */
    public function changePhone(){
        $choseArray = array();
        $user = new UserModel();
        $u_id = input("post.u_id");
        if(empty($u_id)) return apiResponse(201,"用户id不能为空","");
        if(empty(input("post.phone"))){
            return apiResponse(201,"手机号不能为空","");
        }
        if(!empty(input("post.phone")) && empty(input("post.code"))){
            $choseArray = $this->sendSms(input("post.phone"),2,"changephone");
//            $cachename = "phone".$choseArray["data"]["phone"];
//            cache($cachename,$choseArray["data"]["code"],120);//缓存 有效时间 2分钟
            if($choseArray){
                return apiResponse(200,"验证码已发送","");
            }else{
                return apiResponse(201,"验证码发送失败","");
            }
        }
        if(!empty(input("post.phone")) && !empty(input("post.code"))){
            if(input("post.code") == cache("phonechangephone".input("post.phone"))){
                $c["phone"] = input("post.phone");
                $w["u_id"] = $u_id;
                $result = $user -> changePhone($w,$c);
                if($result){
                    cache("phonechangephone".input("post.phone"),NULL); //绑定成功后 清除缓存
                    return apiResponse(200,"更绑成功",$result);
                }else{
                    return apiResponse(201,"更绑失败","");
                }
            }else{
                return apiResponse(201,"验证码已过期或者验证码不正确","");
            }
        }
        if(empty(input("post.phone")) && !empty(input("post.code"))){
            return apiResponse(201,"请输入手机号码","");
        }
    }
    /*
    * Notes: 用户反馈信息
    * Create Date: 2018/10/8
    * Create Time: 下午5:34
    * Create by :张福来
    * param :
    */
    public function addfeedback(){
        $user = new UserModel();
        $w["mix_id"] = input("post.mix_id");
        $w["content"] = input("post.content");
        $w["phone"] = input("post.phone");
        $w["type"] = input("post.type");
        $w["name"] = input("post.name");
        $w["ctime"] = time();
        $w["utime"] = time();
        if(empty($w["mix_id"])) return apiResponse(201,"mix_id 不能为空","");
        if(empty($w["content"])) return apiResponse(201,"反馈内容不能为空","");
        if(empty($w["phone"])) return apiResponse(201,"手机号码 不能为空","");
        if(empty($w["name"])) return apiResponse(201,"反馈人姓名 不能为空","");
        $result = $user ->addfeedback($w);
        if($result){
            return apiResponse(200,"反馈成功等待处理","");
        }else{
            return apiResponse(201,"反馈失败","");
        }
    }
    /*
    * Notes:    保养手册
    * Create Date: 2018/10/23
    * Create Time: 上午10:17
    * Create by :张福来
    * param :
    */
    public function mainmanual(Request $request){
        $order = new Order();
        $postData = $request->post();
        $check = $this->validate($postData,"UserValidate.mainmanual");
        if($check !== true){
            return apiResponse(200,$check,"");
        }
        $w["O.u_id"] = $postData["u_id"];
        $w["O.status"] = ["in","3,4"];
        $page = $postData["pagenum"];
        $result = $order->getByUserIdSelectOrder($w,$page);
        if(!empty($result["list"])){
            return apiResponse(200,"成功",$result);
        }else{
            return apiResponse(200,"数据为空",$result);
        }
    }
}
